function loginToRegister() {
    login = document.getElementById("login");
    login.classList.add("hide");
    register = document.getElementById("register");
    register.classList.remove("hide");
}

function registerToLogin() {
    register = document.getElementById("register");
    register.classList.add("hide");
    login = document.getElementById("login");
    login.classList.remove("hide");
}

function login() {
    var data = {};
    data["account"] = $("#inputAccount").val();
    data["password"] = $("#inputPassword").val();
    $.ajax({
        type: "post",
        url: "/apm/logCheck",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(data),
        success: function (dataResult) {
            if (dataResult.message == "success"){
                window.location.href="/apm/index"
            }else {
                alert(dataResult.message);
            }
        },
        error: function (XMLHttpResponse) {
            alert("操作失败");
        }
    })
}

function regist() {
    var data = {};
    data["registerInputAccount"] = $("#registerInputAccount").val();
    data["registerInputPassword"] = $("#registerInputPassword").val();
    data["registerInputPassword1"] = $("#registerInputPassword1").val();
    data["email"] = $("#email").val();
    if (data.registerInputPassword != data.registerInputPassword1) {
        alert("密码不一致")
        return;
    }
    if (data.registerInputAccount == null || data.registerInputAccount.length <= 0 ||
        data.registerInputPassword == null || data.registerInputPassword.length <= 0 ||
        data.registerInputPassword1 == null || data.registerInputPassword1.length <= 0) {
        alert("账号或密码不能为空")
        return;
    }
    $.ajax({
        type: "post",
        url: "/apm/register",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(data),
        success: function (dataResult) {
            if (dataResult.message == "success"){
                alert("注册成功")
            }else {
                alert(dataResult.message);
            }
        },
        error: function (XMLHttpResponse) {
            alert("操作失败");
        }
    })
}

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function findCpuData(elem) {
    var data = {};
    data["token"] = $(elem).parent("div").parent("div").attr("id");
    data["datetime"] = $(elem).siblings("input").val();
    ctx = $(elem).parent("div").siblings("canvas")[0].getContext("2d");
    $.ajax({
        type: "post",
        url: "/apm/cpuData",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(data),
        success: function (dataResult) {
            if (dataResult.message == "success") {
                var canvasData = {
                    //折线图需要为每个数据点设置一标签。这是显示在X轴上。
                    labels: dataResult.data.fields,
                    //数据集（y轴数据范围随数据集合中的data中的最大或最小数据而动态改变的）
                    datasets: [{
                        label: dataResult.data.model == null ? '当前时间段无数据' : dataResult.data.model,
                        spanGaps: true,
                        borderColor: "#36A2EB", //路径颜色
                        pointBackgroundColor: "#36A2EB", //数据点颜色
                        pointBorderColor: "#fff", //数据点边框颜色
                        data: dataResult.data.data //对应的值
                    }]
                };
                var myLineChart = new Chart(ctx, {
                    type: 'line',
                    data: canvasData,
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero:true
                                }
                            }]
                        },
                        title: {
                            display: true,
                            text: data["token"]+'主机' //标题
                        }
                    }
                });
            }else {
                alert(dataResult.message);
            }
        },
        error: function (XMLHttpResponse) {
            alert("操作失败");
        }
    })
}

function findMemoryData(elem) {
    var data = {};
    data["token"] = $(elem).parent("div").parent("div").attr("id");
    data["datetime"] = $(elem).siblings("input").val();
    ctx = $(elem).parent("div").siblings("canvas")[0].getContext("2d");
    $.ajax({
        type: "post",
        url: "/apm/memoryData",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(data),
        success: function (dataResult) {
            if (dataResult.message == "success") {
                var canvasData = {
                    //折线图需要为每个数据点设置一标签。这是显示在X轴上。
                    labels: dataResult.data.fields,
                    //数据集（y轴数据范围随数据集合中的data中的最大或最小数据而动态改变的）
                    datasets: [{
                        label: "内存占用(GB)",
                        spanGaps: true,
                        borderColor: "#36A2EB", //路径颜色
                        pointBackgroundColor: "#36A2EB", //数据点颜色
                        pointBorderColor: "#fff", //数据点边框颜色
                        data: dataResult.data.data //对应的值
                    }]
                };
                var myLineChart = new Chart(ctx, {
                    type: 'line',
                    data: canvasData,
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero:true
                                }
                            }]
                        },
                        title: {
                            display: true,
                            text: data["token"]+'主机 '+(dataResult.data.model == null ? "当前时间段无数据" : dataResult.data.model)//标题
                        }
                    }
                });
            }else {
                alert(dataResult.message);
            }
        },
        error: function (XMLHttpResponse) {
            alert("操作失败");
        }
    })
}

function findDiskData(elem) {
    var data = {};
    data["token"] = $(elem).parent("div").parent("div").attr("id");
    data["datetime"] = $(elem).siblings("input").val();
    ctx = $(elem).parent("div").siblings("canvas")[0].getContext("2d");
    $.ajax({
        type: "post",
        url: "/apm/diskData",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(data),
        success: function (dataResult) {
            if (dataResult.message == "success") {
                var datasets = [];
                for( let key in dataResult.data.data){
                    var dataset = {
                        label: dataResult.data.model[key],
                        spanGaps: true,
                        borderColor: "#36A2EB", //路径颜色
                        pointBackgroundColor: "#36A2EB", //数据点颜色
                        pointBorderColor: "#fff", //数据点边框颜色
                        data: dataResult.data.data[key] //对应的值
                    };
                    datasets.push(dataset);
                }
                var canvasData = {
                    //折线图需要为每个数据点设置一标签。这是显示在X轴上。
                    labels: dataResult.data.fields,
                    //数据集（y轴数据范围随数据集合中的data中的最大或最小数据而动态改变的）
                    datasets: datasets
                };
                var myLineChart = new Chart(ctx, {
                    type: 'line',
                    data: canvasData,
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero:true
                                }
                            }]
                        },
                        title: {
                            display: true,
                            text: data["token"]+'主机 '+(dataResult.data.model == null ? "当前时间段无数据" : "各分区磁盘使用情况")//标题
                        }
                    }
                });
            }else {
                alert(dataResult.message);
            }
        },
        error: function (XMLHttpResponse) {
            alert("操作失败");
        }
    });
}

function delToken(elem) {
    var data = {};
    console.log($(elem).parent("td").siblings(".token"));
    data["token"] = $(elem).parent("td").siblings(".token").text();

    Ewin.confirm({ message: "确认要删除选择的数据吗？删除后该token下的数据将全部丢失！！！" }).on(function (e) {
        if (!e) {
            return;
        } else {
            $.ajax({
                type: "post",
                url: "/apm/delToken",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(data),
                success: function (dataResult) {
                    if (dataResult.message == "success") {
                        Ewin.alert({message: "删除成功"});
                        location.reload();
                    }else {
                        alert(dataResult.message);
                    }
                },
                error: function (XMLHttpResponse) {
                    alert("操作失败");
                }
            })
        }
    });
}

function createToken() {
    Ewin.confirm({ message: "确认要新建一个token么？！" }).on(function (e) {
        if (!e) {
            return;
        } else {
            $.ajax({
                type: "post",
                url: "/apm/createToken",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (dataResult) {
                    if (dataResult.message == "success") {
                        Ewin.alert({message: "创建成功"});
                        location.reload();
                    }else {
                        alert(dataResult.message);
                    }
                },
                error: function (XMLHttpResponse) {
                    alert("操作失败");
                }
            })
        }
    });
}

function downloadFile(fileName) {
    window.location.href="downloadProbe/"+fileName;
}